package com.aletto.general_store.ui.popup;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.aletto.general_store.R;
import com.aletto.general_store.data.model.Goods;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.CenterPopupView;
import com.lxj.xpopup.util.XPopupUtils;

public class GoodsBuyStatePopup extends CenterPopupView {

    private  int number;

    private Goods     goods;
    private ImageView ivState;


    public GoodsBuyStatePopup(@NonNull Context context) {
        super(context);
    }

    public GoodsBuyStatePopup(@NonNull Context context, Goods goods, int number) {
        super(context);
        this.goods = goods;
        this.number = number;
    }

    @Override
    protected int getImplLayoutId() {

        return R.layout.popup_goods_buy_state;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        ivState = findViewById(R.id.iv_state);

        // 加载动画
        startAnimator(ivState);

    }

    public void playSuccess() {
        ivState.clearAnimation();

        TextView tvState = findViewById(R.id.tv_state);
        tvState.setText(R.string.goods_buy_success);
        ivState.setImageResource(R.mipmap.ic_buy_success);

        // 成功
        dismiss();
        new XPopup.Builder(getContext()).asCustom(new PaySuccessPopup(getContext(), goods, number)).show();
    }

    private void startAnimator(View view) {
        Animation rotationAnimation = AnimationUtils.loadAnimation(view.getContext(), R.anim.anim_pay_loading);
        ivState.startAnimation(rotationAnimation);
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
        ivState.clearAnimation();
    }

    @Override
    protected int getMaxWidth() {
        if (this.popupInfo == null) {
            return 0;
        } else {
            return this.popupInfo.maxWidth == 0 ? (int) ((float) XPopupUtils.getAppWidth(this.getContext()) * 0.7F) : this.popupInfo.maxWidth;
        }
    }

}
